package com.tyy.springbootmysql.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tyy.springbootmysql.entity.UserInfo;
import com.tyy.springbootmysql.mapper.UserInfoMapper;
import com.tyy.springbootmysql.util.ThreadPoolUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


// import static org.apache.logging.log4j.message.MapMessage.MapFormat.JSON;


@Service
@Slf4j
public class UserInfoService extends ServiceImpl<BaseMapper<UserInfo>, UserInfo> {
    @Autowired
    private UserInfoServiceAsync userInfoServiceAsync;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean saveUserInfo(UserInfo userInfo) {
        boolean result = userInfoMapper.insert(userInfo) > 0;
        log.info("saveUserInfo: {}", result);
        return result;
    }
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean updateUserInfo(UserInfo userInfo) {
        log.info("updateUserInfo: {}", userInfo);
        // 使用lambda表达式创建并启动线程
        // Thread thread = new Thread(() -> {
        //     System.out.println("线程正在运行...");
        //     // 在这里添加你的线程任务
        //     try {
        //         userInfoServiceAsync.updateUserInfo(userInfo);
        //     } catch (Exception e) {
        //         log.error("updateUserInfo error: {}", e.getMessage());
        //     }
        //     System.out.println("线程结束...");
        // });
        // thread.start();
        ThreadPoolUtil.poolTaskExecutor.execute(() -> {
            try{
                log.info("线程开始执行...");
                userInfoServiceAsync.updateUserInfo(userInfo);
                log.info("线程结束执行...");
            } catch (Exception e) {
                log.error("saveUserInfo error: {}", e.getMessage());

            }
        });



        UserInfo userInfo1 = new UserInfo();
        // userInfo1.setUserId(1L);
        userInfo1.setRealName("tyy0");
        userInfo1.setPassword("000000");
        userInfo1.setSex("0");
        boolean result = userInfoMapper.insert(userInfo1) > 0;
        // boolean result = userInfoMapper.updateById(userInfo1) > 0;
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("updateUserInfo: {}", result);
        // userInfoServiceAsync.updateUserInfo(userInfo);
        return result;
    }
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean deleteUserInfo(Long id) {
        boolean result = userInfoMapper.deleteById(id) > 0;
        log.info("deleteUserInfo: {}", result);
        return result;
    }

    public UserInfo getUserInfo(Long id) {
        UserInfo userInfo = userInfoMapper.selectById(id);
        log.info("getUserInfo: {}", JSON.toJSONString(userInfo));
        UserInfo info = this.getById(id);
        log.info("info: {}", JSON.toJSONString(info));
        return userInfo;
    }


}
